package bsh.meta;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.objectweb.asm.Type;

/**
 * {@link MethodDescriptor} para um java {@link Method}
 * 
 * @author takeshi
 */
public class JavaMethodDescriptor extends AbstractMethodDescriptor implements MethodDescriptor {

    private final Method method;

    /**
     * @param method {@link Method}
     */
    public JavaMethodDescriptor(Method method) {
        super();
        this.method = method;
    }

    /**
     * {@inheritDoc}
     */
    public String methodName() {
        return this.method.getName();
    }

    /**
     * {@inheritDoc}
     */
    public int modifiers() {
        return this.method.getModifiers();
    }

    /**
     * {@inheritDoc}
     */
    public TypeWrapper returnType() {
        return JavaClassWrapper.wrapperFor(this.method.getReturnType());
    }

    /**
     * {@inheritDoc}
     */
    public List<? extends TypeWrapper> throwsClauses() {
        List<TypeWrapper> list = new LinkedList<TypeWrapper>();
        Class<?>[] exceptionTypes = this.method.getExceptionTypes();
        for (Class<?> excType : exceptionTypes) {
            list.add(JavaClassWrapper.wrapperFor(excType));
        }
        return list;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toDescription() {
        return Type.getMethodDescriptor(this.method);
    }

    /**
     * {@inheritDoc}
     */
    public TypeWrapper declaringType() {
        return JavaClassWrapper.wrapperFor(this.method.getDeclaringClass());
    }

    /**
     * {@inheritDoc}
     */
    public List<? extends TypeWrapper> parameterTypes() {
        Class<?>[] args = this.method.getParameterTypes();
        List<TypeWrapper> list = new ArrayList<TypeWrapper>(args.length);
        for (Class<?> class1 : args) {
            list.add(JavaClassWrapper.wrapperFor(class1));
        }
        return list;
    }

    /**
     * {@inheritDoc}
     */
    public boolean isVarArgs() {
        return this.method.isVarArgs();
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Object#hashCode()
     */

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.method == null) ? 0 : this.method.hashCode());
        return result;
    }

    /**
     * {@inheritDoc}
     * 
     * @see java.lang.Object#equals(java.lang.Object)
     */

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (this.getClass() != obj.getClass()) {
            return false;
        }
        JavaMethodDescriptor other = (JavaMethodDescriptor) obj;
        if (this.method == null) {
            if (other.method != null) {
                return false;
            }
        } else if (!this.method.equals(other.method)) {
            return false;
        }
        return true;
    }

    /**
     * Finds a method withe the given signatura
     * 
     * @param host Class
     * @param methodName String
     * @param paramTypes Class of parameter types
     * @return {@link JavaMethodDescriptor}
     * @throws NoSuchMethodException e
     */
    public static JavaMethodDescriptor findMethod(Class<?> host, String methodName, Class<?>... paramTypes) throws NoSuchMethodException {
        return new JavaMethodDescriptor(host.getMethod(methodName, paramTypes));
    }

}
